
#include "EncoderConsole.h"

int EncoderConsole::get_channel_num(){
    std::lock_guard<std::mutex> lock(enc_map_mtx_);
    return enc_map_.size();
}


void EncoderConsole::addChannel(std::string url, int dev_id){
    int channel_idx = get_channel_num();
    enc_map_.insert(std::make_pair(channel_idx,std::make_shared<ChannelEncoder>(url,dev_id,que_size_)));
    enc_map_[channel_idx]->start();
    std::cout<<"Decoder channel_idx "<<channel_idx<<" start!"<<std::endl;
}


int EncoderConsole::read(int channel_idx, std::shared_ptr<bm_image>& out_image){
    std::lock_guard<std::mutex> lock(enc_map_mtx_);
    if(enc_map_.find(channel_idx)==enc_map_.end()){
        std::cout<<"error!channel_idx doesn't exist!"<<std::endl;
        return -1;
    }
    return enc_map_[channel_idx]->get_image(out_image);
}

void EncoderConsole::push_img(int channel_idx, std::shared_ptr<bm_image> img_ptr){
    std::lock_guard<std::mutex> lock(enc_map_mtx_);
    if(enc_map_.find(channel_idx)==enc_map_.end()){
        std::cout<<"error!channel_idx doesn't exist!"<<std::endl;
        return ;
    }
    enc_map_[channel_idx]->push_image(img_ptr);
}